﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ExportCommand;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.InventoryCommand;
using B.S.BaseData.Manage.Api.Read.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.BaseData.Manage.Api.Read.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventoryController : ControllerBase
    {
        private readonly IMediator mediator;

        public InventoryController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 库存清单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ApiReqult<ApIPageing<InventoryQuserDTO>>> InventoryQuser([FromQuery] InventoryQuserCommand request)
        {
            return await mediator.Send(request);
        }
        [HttpGet]
        public async Task<ApiReqult<List<InventoryQuserDTO>>> ExportQuser([FromQuery] ExportQuserCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="commodityIds"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportOrderList([FromQuery] List<string> Product_id)
        {
            if (Product_id == null || !Product_id.Any())
            {
                return BadRequest("请提供商品 ID 列表。");
            }

            var request = new ExportQuserCommand { Product_id = Product_id };
            var result = await ExportQuser(request);

            if (result.Data?.Any() != true)
                return NotFound("未找到商品数据。");

            using IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("商品列表");
            IRow headerRow = sheet.CreateRow(0);

            // 设置表头
            string[] headers = { "产品编号", "产品名称", "产品规格", "类别", "存储单位", "仓库名称", "库位名称", "库位类型", "库存数量", "库存上限", "库存下限", "操作日期", "入库数量", "出库数量", "产品批次编号", "操作备注" };
            for (int i = 0; i < headers.Length; i++)
                headerRow.CreateCell(i).SetCellValue(headers[i]);

            // 填充数据行
            int rowIndex = 1;
            foreach (var item in result.Data) // 直接使用 result.Data 集合
            {
                IRow row = sheet.CreateRow(rowIndex++);
                row.CreateCell(0).SetCellValue(item.Product_id ?? "");
                row.CreateCell(1).SetCellValue(item.Product_name ?? "");
                row.CreateCell(2).SetCellValue(item.Spec ?? "");
                row.CreateCell(3).SetCellValue(item.Category ?? "");
                row.CreateCell(4).SetCellValue(item.Storage_unit ?? "");
                row.CreateCell(5).SetCellValue(item.Warehouse_name ?? "");
                row.CreateCell(6).SetCellValue(item.Location_name ?? "");
                row.CreateCell(7).SetCellValue(item.Location_type ?? "");
                row.CreateCell(8).SetCellValue(item.Stock_quantity ?? 0);
                row.CreateCell(9).SetCellValue(item.upper_limit ?? 0);
                row.CreateCell(10).SetCellValue(item.lower_limit ?? 0);
                row.CreateCell(11).SetCellValue(item.Operation_date.ToString("yyyy-MM-dd HH:mm:ss"));
                row.CreateCell(12).SetCellValue(item.In_quantity);
                row.CreateCell(13).SetCellValue(item.Out_quantity);
                row.CreateCell(14).SetCellValue(item.Batch_no ?? "");
                row.CreateCell(15).SetCellValue(item.Remark ?? "");
            }

            using var ms = new MemoryStream();
            book.Write(ms);
            byte[] buffer = ms.ToArray();

            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "商品列表.xlsx");
        }
    }
}